package sf.database.jdbc.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.time.OffsetTime;

public class OffsetTimeType implements TypeHandler<OffsetTime> {
    public OffsetTime get(ResultSet rs, String columnName) throws SQLException {
        return rs.getObject(columnName, OffsetTime.class);
    }

    public OffsetTime get(ResultSet rs, int index) throws SQLException {
        return rs.getObject(index, OffsetTime.class);
    }

    public OffsetTime get(CallableStatement rs, int index) throws SQLException {
        return rs.getObject(index, OffsetTime.class);
    }

    @Override
    public OffsetTime get(CallableStatement cs, String parameterName) throws SQLException {
        return cs.getObject(parameterName, OffsetTime.class);
    }

    @Override
    public void update(ResultSet rs, String columnLabel, Object value) throws SQLException {
        rs.updateObject(columnLabel, value);
    }

    @Override
    public void update(ResultSet rs, int columnIndex, Object value) throws SQLException {
        rs.updateObject(columnIndex, value);
    }

    public Object set(PreparedStatement stat, Object obj, int i) throws SQLException {
        if (null == obj) {
            stat.setNull(i, getSqlType());
        } else {
            stat.setObject(i, obj);
        }
        return null;
    }

    @Override
    public Object set(CallableStatement cs, Object obj, String parameterName) throws SQLException {
        if (null == obj) {
            cs.setNull(parameterName, getSqlType());
        } else {
            cs.setObject(parameterName, obj);
        }
        return null;
    }

    @Override
    public int getSqlType() {
        return Types.TIME_WITH_TIMEZONE;
    }

    @Override
    public Class<OffsetTime> getDefaultJavaType() {
        return OffsetTime.class;
    }
}
